<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0 minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <title>我的问卷</title>
    <!--<link href="../public/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">-->
    <link href="public/css/header.css" rel="stylesheet" type="text/css">
    <link href="public/css/container.css" rel="stylesheet" type="text/css">
    <script src="../public/jQuery/jQuery-2.1.3.min.js"></script>
    <script src="../public/js/publicjs.js"></script>
    <script src="public/js/myjs/whole.js"></script>
    <style>

        .main__bg{
            background: #EDF0F0;
        }
        section .img_no_content{
            width: 15.625vh;
            height: 15.625vh;
            /*position: relative;*/
            margin-top:28vh;
        }

        .subjects{
            position: relative;
        }
        .subjects .subject{
            border-bottom: 1px solid #E5E5E5;
            border-top: 1px solid #E5E5E5;
            /*height: 18.75vh;*/
        }
        .subject_edit{
            position:relative;
            height: 9.375vh;
            /*height: 10rem;*/
            background: #CCCCCC;
            display: none;
        }
        .subject + .subject{
            border-top:0px solid #E5E5E5;
        }

        .subject_title{
            width: 100%;
            height:9.375vh;
            /*height:10rem;*/
        }

        .subjects .subject .title{
            position: relative;
            /*height: 5rem;*/
            /*line-height: 6rem;*/
            /*margin-left: 1.67rem;*/
            height: 4.6875vh;
            line-height: 5.625vh;
            /*margin-left: 1.5625vw;*/
            margin-left: 1.5625vh;
            color: #323232;
        }

        .subjects .subject .type{
            position: relative;
            /*height: 5rem;*/
            /*line-height: 4rem;*/
            /*font-size: 2.167rem;*/
            /*margin-left: 1.67rem;*/
            height: 4.6875vh;
            line-height: 3.75vh;
            font-size: 2.03125vh;
            /*margin-left: 1.5625vw;*/
            margin-left: 1.5625vh;
            color: #808080;

        }
        .subjects .subject_edit .modify{
            width: 25%;
            text-align: center;
            float: left;
        }
        .subjects .subject_edit .modify2{
            width: 25%;
            text-align: center;
            float: left;
        }
        .subjects .subject_edit .modify2 img{
            /*height: 4.5rem;*/
            /*width:4.833rem;*/
            /*padding-top:1.67rem;*/
            height: 4.21875vh;
            /*width: 4.53125vh;*/
            width: 4.53125vh;
            padding-top: 1.5625vh;
            display: block;
            margin: 0 auto;
        }
        .count{
            position: absolute;
            top:0px;
            text-align: center;
            /*width: 8rem;*/
            /*right: 1.67rem;*/
            /*width: 7.5vw;*/
            width: 7.5vh;
            right: 1.5625vh;
            color: #18CC9C;
        }
        .state{
            position: absolute;
            /*right: 1.67rem;;*/
            right: 1.5625vh;
        }
        .operation{
            color: #FFFFFF;
            /*padding-top: 0.5rem;*/
            /*font-size: 1.67rem;*/
            padding-top: 0.46875vh;
            /*font-size: 1.5625vw;*/
            font-size: 1.5625vh;
        }

        /*撤销*/
        .call_back
        {
            display: none;
            position: absolute;
            z-index: 99;
            width: 100%;
            height: 100vh;
            padding-top: 38vh;
            background: rgba(0, 0, 0, 0.4);
        }
        .content_big
        {
            width: 83.33vw;
            height: 15.625vh;
            margin-left: 8.333vw;
            background: white;
            border-radius: 15px;
        }
        .more_again
        {
            width: 83.33vw;
            margin-left: 8.333vw;
            background: white;
            border-radius: 15px;
            height: 23.4375vh;
        }
        .popup_similar
        {
            width: 100%;
            height: 7.8125vh;
            line-height: 7.8125vh;
            font-size: 4.5vw;
            text-align: center;
            border-bottom: 1px solid lightgrey;
            color: black;
        }
        .choice_big
        {
            width: 100%;
            height: 7.8125vh;
        }
        .okCancel
        {
            width: 100%;
            height: 7.8125vh;
        }
        .only_cancel
        {
            width: 100%;
            height: 7.8125vh;
            font-size: 4.5vw;
            color: #1091FF;
            line-height: 7.8125vh;
            text-align: center;
        }
        .choice_it
        {
            width: 49%;
            height: 100%;
            font-size: 4.5vw;
            color: #1091FF;
            line-height: 7.8125vh;
            text-align: center;
            float: left;
        }
        .call_back_success {
            display: none;
            width: 55.56vw;
            height: 7.8125vh;
            background-color: #333333;
            border-radius: 15px;
            font-size: 4.5vw;
            line-height: 7.8125vh;
            padding-left: 2vw;
            color: white;
            margin-left: 23vw;
        }
        .text_no_more{
            position: relative;
            display: block;
            text-align: center;
            color: #B2B2B2;
            padding:2vh;
        }
        .text_no_more .text_no_more_q{
            display: none;
            padding:2vh;
        }
        .no_send_question{
            color: #F5D6C9;
            line-height: 2.8125vh;
            font-size: 2.031vh;
        }
    </style>

</head>
<body>
<header>
    <div class="header__box tl">

        <img onclick="exit()" src="public/img/basic/back.png" />
        <span onclick="exit()" class="back">返回</span>

    </div>
    <div class="header__box tc header__box__title">
        我的问卷
    </div>

    <div class="header__box tr">

        <span onclick="goAdd()" class="newCreate">
            新&nbsp;建
        </span>

    </div>

    <div class="call_back" data-type="1" data-questionnaire_id="1" data-questionnaire_title="">
        <div class="content_big">
            <div class="popup_similar title_big">

            </div>
            <div class="choice_big">
                <div class="okCancel">
                    <div class="choice_it cancel" style="border-right: 1px solid lightgrey;width: 49%;">取消</div>
                    <div class="choice_it" id="sure">确定</div>
                </div>
            </div>
        </div>
    </div>

</header>
<section class="">
    <div class="content">
        <div class="subjects">

        </div>
        <div class="text_no_more">
            <img class="loading" src='public/img/basic/loading.gif'>
            <span class="text_no_more_q">
                没有更多问卷了...
            </span>
        </div>
    </div>
</section>

</body>
</html>

<script>

    $(function(){
        //第一次进来
        load();

        var stop=true;//触发开关，防止多次调用事件
        $(window).scroll(function() {
            //当内容滚动到底部时加载新的内容 100当距离最底部100个像素时开始加载.
            if ($(this).scrollTop() + $(document).height() +100>= $(document).height()) {

                if (stop == true) {
                    stop = false;
                    load();
                }
            }
        });

        function load(){
            //没有问卷的话就传0,有的话传最后一个问卷的时间戳
            var timestamp = $(".subjects .subject").last().attr('data-timestamp')==undefined?0:$(".subjects .subject").last().attr('data-timestamp');
            var data={
                publish_id:user_id,
                timestamp:timestamp
            };
            $.ajax({
                type: 'GET',
                url: apiurl+'/questionnaires/list',
                dataType: 'json',
                data:data,
                beforeSend:function(){

                    $('.text_no_more .loading').css('display','inline');
                },
                success: function(data){
                    var d= JSON.parse(data);
                    var html='';
                    if(d.data.length>0){
                        for(var i=0;i< d.data.length;i++){
                            console.log(d.data[i].questionnaire_name);
                            d.data[i].questionnaire_name=HTMLEncode(d.data[i].questionnaire_name);
                            html+='<div class="subject" data-questionnaire_status="'+ d.data[i].questionnaire_status+'" data-questionnaire_title="'+d.data[i].questionnaire_name+'" data-questionnaire_id="'+d.data[i].questionnaire_id+'" data-timestamp="'+d.data[i].timestamp+'" data-questionnaire_name="'+d.data[i].questionnaire_name+'" data-questionnaire_welcome="'+d.data[i].questionnaire_welcome+'">'+
                                    '<div class="subject_title">'+
                                    '<div class="title">'+
                                    '<div style="height: 5rem;">'+ d.data[i].questionnaire_name+'</div>';

                            if(d.data[i].questionnaire_status==0){
                                html+= '<span class="count">'+
                                        '</span>';
                            }
                            else {
                                html+= '<div class="count">'+d.data[i].collectionCount+
                                        '</div>';
                            }

                            html+= '</div>'+
                                    '<div class="type">';

                            initDate(new Date(d.data[i].timestamp));
                            html+='<span>'+ year_mon_day +'</span>';

                            if(d.data[i].questionnaire_status==0){
                                html+='<span class="state">未发布'+'</span>';
                            }
                            else {
                                html+='<span class="state">收集中'+'</span>';
                            }
                            html+= '</div>'+
                                    '</div>'+
                                    '<div class="subject_edit">'+
                                    '<div class="modify2 modify2_edit">';

                            if(d.data[i].questionnaire_status==0){
                                html+='<img src="public/img/edit/edit_edit.png">'+
                                        '<div class="operation">编辑'+
                                        '</div>'+
                                        '</div>'+
                                        '<div class="modify2 modify2_publish">'+
                                        '<img src="public/img/edit/publish.png">'+
                                        '<div class="operation">'+
                                        '发布'+
                                        '</div>'+
                                        '</div>';
                            }
                            else{
                                html+='<img src="public/img/edit/detail.png">'+
                                        '<div class="operation">详情'+
                                        '</div>'+
                                        '</div>'+
                                        '<div class="modify2 modify2_publish">'+
                                        '<img src="public/img/edit/publish.png">'+
                                        '<div class="operation">'+
                                        '分享'+
                                        '</div>'+
                                        '</div>';
                            }
                            html+='<div class="modify2 modify2_result">'+
                                    '<img src="public/img/edit/result.png">'+
                                    '<div class="operation">'+
                                    '结果'+
                                    '</div>'+
                                    '</div>'+
                                    '<div class="modify2 modify2_delete">'+
                                    '<img src="public/img/edit/edit_delete.png">'+
                                    '<div class="operation">'+
                                    '删除'+
                                    '</div>'+
                                    '</div>'+
                                    '</div>'+
                                    '</div>';
                        }
                        $('.subjects').append(html);
                    }
                    else {
                        if($('.subjects .subject').length==0){
                            html+='<div class="container">' +
                                    '<img class="img_no_content" src="public/img/pre/no_content.png">' +
                                    '<div style="height: 2vh;">'+
                                    '</div>'+
                                    '<span class="no_send_question">'+
                                    '你还未发起过问卷'+
                                    '</span>' +
                                    '</div>';
                        }
                        $('.content').append(html);
                    }


                    if(d.data.length<10){
                        $('.text_no_more .text_no_more_q').css('display','inline');
                        $(window).unbind('scroll');
                    }
                    if(d.data.length<10 && $('.subjects .subject').length==0){
                        $('.text_no_more .text_no_more_q').css('display','none');
                    }
                    $('.text_no_more .loading').css('display','none');
                    success_operation();
                },
                error: function(xhr, type){
                    stop=true;
                    $('.text_no_more .loading').css('display','none');
                }
            });

        }

        function success_operation(){
            stop=true;
            $(".subjects .subject .subject_title").unbind('click').on('click',function(){
                var d= $(this).next('.subject_edit').css('display');
                if(d=='block'){
                    $(this).next('.subject_edit').hide();
                }
                else {
                    $(this).next('.subject_edit').show();
                    $(this).parent().siblings().children('.subject_edit').hide();
                }
            });
            $(".modify2_edit").unbind('click').on('click',function(){
                var questionnaire_status = $(this).parents('.subject').attr('data-questionnaire_status');
                var questionnaire_id = $(this).parents('.subject').attr('data-questionnaire_id');

                var questionnaire_welcome=$(this).parents('.subject').attr('data-questionnaire_welcome');
                var questionnaire_name=$(this).parents('.subject').attr('data-questionnaire_name');
                if(questionnaire_status==0){
                    location.href='edit.html?questionnaire_id='+questionnaire_id+"&questionnaire_name="+escape(questionnaire_name)+"&questionnaire_welcome="+escape(questionnaire_welcome)+"&type=1";
                }
                else {

                    //问答
                    location.href='answer.html?questionnaire_id='+questionnaire_id;
//                    location.href='edit.html?questionnaire_id='+questionnaire_id+"&questionnaire_name="+escape(questionnaire_name)+"&questionnaire_welcome="+escape(questionnaire_welcome);
                }

            });
            $(".modify2_publish").unbind('click').on('click',function(){
                var questionnaire_status = $(this).parents('.subject').attr('data-questionnaire_status');
                var questionnaire_id = $(this).parents('.subject').attr('data-questionnaire_id');
                var questionnaire_title = $(this).parents('.subject').attr('data-questionnaire_title');
                var questionnaire_welcome = $(this).parents('.subject').attr('data-questionnaire_welcome');

                if(questionnaire_status==0){

                    $('.call_back').attr('data-type','1');//表示发布;
                    $('.call_back').attr('data-questionnaire_id',questionnaire_id);//表示问卷id
                    $('.call_back').attr('data-questionnaire_title',questionnaire_title);//表示问卷标题
                    $('.title_big').text('确认发布此问卷?');
                    $("#sure").text('确认');
                    $('.call_back').css('display','block');
                }
                else {
                    //跳到分享页面,现在是跳到了回答页面
//                    location.href='answer.html?questionnaire_id='+questionnaire_id;

                    shareContent("",group_id,questionnaire_welcome,questionnaire_title,"",3,"","/questionnaire/answer.html?questionnaire_id="+questionnaire_id+"&group_id="+group_id)

                }

            });

            //跳转到结果展示页
            $(".modify2_result").unbind('click').on('click',function(){
                var questionnaire_status = $(this).parents('.subject').attr('data-questionnaire_status');
                var questionnaire_id = $(this).parents('.subject').attr('data-questionnaire_id');
                var questionnaire_title = $(this).parents('.subject').attr('data-questionnaire_title');

                var url='result.html?questionnaire_id='+questionnaire_id+'&questionnaire_name='+ escape(questionnaire_title);
//                location.replace(url);
                location.href='result.html?questionnaire_id='+questionnaire_id+'&questionnaire_name='+ escape(questionnaire_title);
            });
            $(".modify2_delete").unbind('click').on('click',function(){
                var questionnaire_status = $(this).parents('.subject').attr('data-questionnaire_status');
                var questionnaire_id = $(this).parents('.subject').attr('data-questionnaire_id');

                $('.call_back').attr('data-type','0');//表示删除;
                $('.call_back').attr('data-questionnaire_id',questionnaire_id);//表示问卷id
                $('.title_big').text('确定删除此问卷');
                $("#sure").text('确定');
                $('.call_back').css('display','block');

            });
        }

        $('.cancel').on('click',function(){
            $('.call_back').css('display','none');
        });
        $('#sure').on('click',function(){
           var type = $(this).parents('.call_back').attr('data-type');
           var questionnaire_id = $(this).parents('.call_back').attr('data-questionnaire_id');
           var questionnaire_title = $(this).parents('.call_back').attr('data-questionnaire_title');
            //做删除操作
            if(type==0){
                API_Func('/questionnaires/'+questionnaire_id,'DELETE',false,{},function(){

                },function(d){
                    if(d.code==0){
                        $('.call_back').css('display','none');
//                        console.log(d);
                        location.reload();
                    }
                });
            }
            //做发布状态
            else {
                API_Func('/questionnaires/publish/'+questionnaire_id,'PUT',false,{},function(){

                },function(d){
                    if(d.code==0){
                        $('.call_back').css('display','none');
                        location.href='answer.html?questionnaire_id='+questionnaire_id;
//                        location.href='publish_success.html?questionnaire_title='+ escape(questionnaire_title);
                    }
                });
            }
        });

    });

    function exit (){
        var exit;
        exit = document.createElement('iframe');
        exit.style.display = 'none';
        document.documentElement.appendChild(exit);
        exit.src = "/exit2home";
    }

</script>